﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class EditPlanForm : Form
    {
        public static string constr = ConfigurationManager.AppSettings["ConStr"];
        SqlConnection con = new SqlConnection(constr);
       
        public EditPlanForm(int navCode)
        {
            InitializeComponent();

            if (navCode == Constants.FORM_VIEWALL_CODE)
            {
                buttonEdit.Visible = false;
                buttonDelete.Visible = false;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            this.Hide();
            addplan edit = new addplan(getGridSelectedPlanId());
            // edit.setEditMemberId(getGridSelectedMemberId());
            edit.MdiParent = this.ParentForm;
            edit.Show();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentCell.RowIndex;
                int plan_id = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
                SqlCommand com = new SqlCommand("update subscription_plan set status = 'D' where id=" + plan_id, con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Plan deleted succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadGrid();
            }
            catch
            {
                MessageBox.Show("Plan delete failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                con.Close();
            }
           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void EditPlanForm_Load(object sender, EventArgs e)
        {

            loadGrid();
        }

        private void loadGrid()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("select id,type,amount,duration from subscription_plan where status='A'", con);
            DataTable dt = new DataTable();
            // con.Open();D:\Visual studio\project\GYM\Fitness_club\addinstructor.cs
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
                DialogResult result = MessageBox.Show("No Details found", "Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    this.BeginInvoke(new MethodInvoker(Close));
                }

            }
        }

        private int getGridSelectedPlanId()
        {
            int palanId = -1;
            int index = dataGridView1.CurrentCell.RowIndex;
            palanId = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
            return palanId;
        }
    }
}
